package com.ctshk.rpc.air.service.impl;

import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ctshk.common.dto.Result;
import com.ctshk.common.enums.IsDeletedCode;
import com.ctshk.common.enums.SystemError;
import com.ctshk.common.exception.BusinessException;
import com.ctshk.common.utils.EntityUtil;
import com.ctshk.rpc.air.dto.AirProductSaleRecordDTO;
import com.ctshk.rpc.air.entity.AirProductCost;
import com.ctshk.rpc.air.entity.AirProductSaleRecord;
import com.ctshk.rpc.air.mapper.AirProductCostMapper;
import com.ctshk.rpc.air.mapper.AirProductSaleRecordMapper;
import com.ctshk.rpc.air.req.AirProductSaleRecordAddReq;
import com.ctshk.rpc.air.service.IAirProductSaleRecordService;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Objects;
import java.util.Optional;

/**
 * <p>
 * 产品库存售卖记录 服务实现类
 * </p>
 *
 * @author 谢诗宏
 * @since 2021-03-18
 */
@DubboService
public class AirProductSaleRecordServiceImpl extends ServiceImpl<AirProductSaleRecordMapper, AirProductSaleRecord> implements IAirProductSaleRecordService {

    @Autowired
    private AirProductSaleRecordMapper airProductSaleRecordMapper;
    @Resource
    private AirProductCostMapper airProductCostMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Result add(AirProductSaleRecordAddReq req, Long userId) {
        //先判断 同一个人 是否已经有了售卖记录
        Long id = IdWorker.getId();
        AirProductSaleRecord airProductSaleRecord = EntityUtil.copy(req, AirProductSaleRecord.class);
        airProductSaleRecord.setId(id);
        airProductSaleRecord.setGmtCreate(LocalDateTime.now());
        airProductSaleRecord.setCreateId(userId);
        airProductSaleRecord.setIsDeleted(IsDeletedCode.NO.getCode());
        airProductSaleRecord.setModifiedId(userId);
        airProductSaleRecord.setGmtModified(LocalDateTime.now());
        int insert = airProductSaleRecordMapper.insert(airProductSaleRecord);
        if (insert < 1)
            throw new BusinessException(SystemError.SYS_437);
        return Result.success(id);
    }

    @Override
    public boolean removeByProductAndOrderId(Long productId, Long orderId) {
        boolean remove = remove(Wrappers.<AirProductSaleRecord>lambdaQuery()
                .eq(AirProductSaleRecord::getProductId, productId)
                .eq(AirProductSaleRecord::getOrderId, orderId)
        );
        return remove;
    }

    @Override
    public List<AirProductSaleRecordDTO> getByProductId(Long productId, LocalDate date) {
        List<AirProductSaleRecord> list = list(Wrappers.<AirProductSaleRecord>lambdaQuery()
                .eq(AirProductSaleRecord::getProductId, productId)
                .eq(AirProductSaleRecord::getSalesDate, date)
        );
        return EntityUtil.copyList(list, AirProductSaleRecordDTO.class);
    }

    @Override
    public Integer getRemainingInventory(Long productId, LocalDate date) {
        //获取成本
        //获取当前可用成本
        AirProductCost cost = airProductCostMapper.getCurrentlyValidCost(productId, date);
        if (Objects.nonNull(cost)) {
            Integer dailyStock = cost.getDailyStock();
            //获取已售卖数量
            Optional<Integer> sum = list(Wrappers.<AirProductSaleRecord>lambdaQuery()
                    .eq(AirProductSaleRecord::getProductId, productId)
                    .eq(AirProductSaleRecord::getSalesDate, date)
            ).stream().map(AirProductSaleRecord::getSoldNumber).reduce(Integer::sum);
            return dailyStock - sum.get();
        }
        return 0;
    }
}
